import QtQuick 2.0
import QtMultimedia 5.0

// Credits
// bin icon : http://findicons.com/icon/158568/trash_recyclebin_empty_closed?id=350118
// beep sound: http://www.freesound.org/people/zerolagtime/sounds/144418/

Item {
    property int margin: 20
    property int entryHeight: 100
    property int numEntries: 6
    property double estCost: 0
    property string lastUPC

    Row {
        width: parent.width - margin

        Column {
            id: groceriesList
            width: parent.width - scannerCol.width
            spacing: 50

            Column {
                id: headerCol
                spacing: 10
                width: parent.width
                Text {
                    id: header
                    text: "Grocery List"
                    font.family: "Verdana"
                    anchors.horizontalCenter: parent.horizontalCenter
                    font.pointSize: 30
                }

                Text {
                    id: cost
                    text: "Estimated Cost: $" + estCost
                    font.family: "Verdana"
                    font.pointSize: 25
                    font.italic: true
                    anchors.horizontalCenter: parent.horizontalCenter
                    color: "grey"

                }
            }

            Flickable {
                width: parent.width
                height: 690
                contentHeight: (entryHeight + 10) * numEntries
                clip: true

                Column {
                    id: entries
                    width: parent.width
                    x: 20

                    spacing: 10

                }
            }
        }

        Column {
            id: scannerCol
            width: 650
            height: parent.height
            anchors.topMargin: margin
            spacing: margin

            Text {
                text: "Barcode Scanner"
                font.family: "Verdana"
                font.pointSize: 30
                anchors.horizontalCenter: parent.horizontalCenter
            }

            Rectangle {
                width: parent.width
                height: 490
                color: content.border.color

                Rectangle {
                    id: cameraRect
                    width: 640
                    height: 480
                    anchors.centerIn: parent


                    VideoOutput {
                        anchors.fill: parent
                        source: camera
                        focus: visible // to receive focus and capture key events when visible
                    }
                }
            }
        }

    }


    Timer {
        interval: 5000 // 5 seconds
        running: true
        repeat: true
        triggeredOnStart: true
        onTriggered: {

            if( groceriesContent.visible ) {
                var upc = barcodeScanner.recognize();
                if( upc !== "" && upc !== lastUPC) {
                    pollData( upc );
                    lastUPC = upc;
                    beepSound.play();
                }
            }
        }
    }

    Camera {
        id: camera
    }

    Audio {
            id: beepSound
            source: "sounds/scannerBeep.mp3"
            autoPlay: false
        }

    function pollData(upc) {
        var doc = new XMLHttpRequest();
        doc.onreadystatechange = function() {
            if (doc.readyState == XMLHttpRequest.DONE) {
                var jsonObject = eval('(' + doc.responseText + ')');
                var component = Qt.createComponent("GroceryEntry.qml");
                var sprite = component.createObject(entries, {
                                                        "imageUrl": jsonObject.response.data[0].image_urls[0],
                                                        "brand": jsonObject.response.data[0].brand,
                                                        "productName": jsonObject.response.data[0].product_name,
                                                        "size": jsonObject.response.data[0].size[0],
                                                        "price": parseFloat(jsonObject.response.data[0].avg_price)
                                                    });
                groceriesContent.estCost = groceriesContent.estCost + parseFloat(jsonObject.response.data[0].avg_price);
                groceriesContent.estCost = groceriesContent.estCost.toFixed(2);
            }

        }
        doc.open("GET", "http://api.v3.factual.com/t/products-cpg-nutrition?q=" + upc + "&KEY=OvwQBRfLQS72IC2RBbp3T29PbbuVeSny4EieaeCd");
        doc.send();
    }
}
